import { ClassifyItem } from '@/api/premium/category/model/classifyModel'
import { AtlasIsShow } from '@/api/premium/content/model/atlasModel'
import { getRandomIntInclusive } from '@/utils/numberHelper'
import {
  MaterialSource,
  MaterialStucture,
  MaterialType,
  MaterialSecType,
  MaterialStatus,
  ModifyMaterialAtlasItem,
  MaterialUsage,
} from '../MaterialList/types'

export default function useFormat() {
  const formatUsage = (usage: MaterialUsage) => {
    const sourceMap: { [value in MaterialUsage]: string } = {
      [MaterialUsage.BUSINESS]: '商业',
      [MaterialUsage.MEDIA]: '传媒',
    }
    return sourceMap[usage]
  }
  /**
   * 格式化素材来源
   */
  const formatSource = (source: MaterialSource) => {
    const sourceMap: { [value in MaterialSource]: string } = {
      [MaterialSource.PLUS]: 'PLUS',
      [MaterialSource.DP]: 'DP',
      [MaterialSource.BEST_IMG]: '优图佳视',
      [MaterialSource.OPEN_PIECE]: '开篇',
    }
    return sourceMap[source]
  }

  /**
   * 格式化构图方式
   */
  const formatStructure = (stucture: MaterialStucture) => {
    const nodeWrapper = (text: string) => <strong>{text}</strong>
    const stuctureMap: { [value in MaterialStucture]: string } = {
      [MaterialStucture.UNKNOW]: '未知',
      [MaterialStucture.HORIZONTAL]: '横图',
      [MaterialStucture.VERTICAL]: '竖图',
      [MaterialStucture.SQUARE]: '方图',
      [MaterialStucture.PANORAMA]: '全景图',
    }
    return nodeWrapper(stuctureMap[stucture])
  }

  /**
   * 格式化素材类型
   */
  const formatType = (type: MaterialType, itype: MaterialSecType) => {
    const typeMap: { [value in MaterialType]: string } = {
      [MaterialType.IMAGE]: '图片',
      [MaterialType.VIDEO]: '视频',
      [MaterialType.MUSIC]: '音乐',
      [MaterialType.FONT]: '字体',
    }

    const itypeMap: { [value in MaterialSecType]: string } = {
      [MaterialSecType.NONE]: '无',
      [MaterialSecType.PHOTOGRAPHY]: '摄影图',
      [MaterialSecType.AVOID_CUTOUT]: '免扣',
      [MaterialSecType.VECTORGRAPH]: '设计矢量图',
    }
    return `${typeMap[type]}-${itypeMap[itype] || '无'}`
  }

  /**
   * 格式化审核状态
   */
  const formatStatus = (status: MaterialStatus) => {
    const statusMap: { [value in MaterialStatus]: JSX.Element } = {
      [MaterialStatus.NORMAL]: <a-badge status="success" text="正常" />,
      [MaterialStatus.FORBIDDEN]: <a-badge status="default" text="已下架" />,
      [MaterialStatus.CHECK_PENDING]: (
        <a-badge status="warning" text="待审核" />
      ),
      [MaterialStatus.REJECT]: <a-badge status="error" text="驳回" />,
    }
    return statusMap[status]
  }

  const formatAtlasShow = (status: AtlasIsShow) => {
    const isShowMap: EnumToO<AtlasIsShow, JSX.Element> = {
      [AtlasIsShow.HIDDEN]: <a-badge status="default" text="不显示" />,
      [AtlasIsShow.SHOW]: <a-badge status="processing" text="显示" />,
    }
    return isShowMap[status]
  }

  const formatTag = (tags: unknown[], type: 'tag' | 'atlas' | 'categories') => {
    const colorList = [
      'pink',
      'red',
      'orange',
      'green',
      'cyan',
      'blue',
      'purple',
    ]
    if (tags.length <= 0) {
      return '暂无'
    }
    if (type === 'atlas') {
      const tagsList = tags as string[]
      return tagsList.map((item) => <a-tag color="#108ee9">{item}</a-tag>)
    }
    if (type === 'categories') {
      const tagsList = tags as ClassifyItem[]
      return tagsList.map((item) => <a-tag color="#2db7f5">{item.name}</a-tag>)
    }
    if (type === 'tag') {
      const tagsList = tags as string[]
      return tagsList.map((item) => (
        <a-tag color={colorList[getRandomIntInclusive(0, 6)]}>{item}</a-tag>
      ))
    }
    return tags.map((item) => <a-tag>{item}</a-tag>)
  }

  const formatAtlasTag = (tags: ModifyMaterialAtlasItem[]) => {
    if (tags.length <= 0) {
      return '暂无'
    }
    return tags.map((item) => {
      if (item) {
        return <a-tag color="#3b5999">{item.name}</a-tag>
      }
      return ''
    })
  }

  return {
    formatSource,
    formatStructure,
    formatType,
    formatStatus,
    formatTag,
    formatAtlasTag,
    formatAtlasShow,
    formatUsage,
  }
}
